import { legacy_createStore ,applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { STORETYPE,CLEARBTN,EDITBTN ,BAGLIST,TABBTN,ALLTYPE,ALLLIST,PROPSTYPE,LOVETYPE,TYPETYPE} from '../Type/Store.d';


const initialState = {
    list:JSON.parse(localStorage.getItem('list') as any) || [],
    childrenList:JSON.parse(localStorage.getItem('childrenList') as any) || [],
    CopyList:[],
    BagList:JSON.parse(localStorage.getItem('BagList') as any) || [],
}

const routes = (state:STORETYPE = initialState, { type, payload }:ALLTYPE) => {
    const Set_Item = (name:string,con:PROPSTYPE[])=>{
        window.localStorage.setItem(name,JSON.stringify(con))
    }
    switch (type) {

    case ALLLIST:
        state.list = payload;
        state.childrenList = payload[0].children as PROPSTYPE[];
        state.CopyList = payload[0].children as PROPSTYPE[];
        Set_Item('list',state.list);
        Set_Item('childrenList',state.childrenList);

        return { ...state}
    case TABBTN:
        state.childrenList = state.list[payload].children as PROPSTYPE[];
        state.CopyList = state.list[payload].children as PROPSTYPE[];
        Set_Item('childrenList',state.childrenList);
        return { ...state}

    case LOVETYPE:
        let loveCopy = [...state.childrenList];
        if(payload == 1){
            loveCopy = state.CopyList.sort((min:PROPSTYPE,max:PROPSTYPE)=>{
                return (min.love as number) - (max.love as number)
            })
        }else if(payload == 2){
            loveCopy = state.CopyList.sort((min:PROPSTYPE,max:PROPSTYPE)=>{
                return (max.love as number) - (min.love as number)
            })
        }
        state.childrenList = loveCopy
        return { ...state}

    case TYPETYPE:
        let typeCopy = [...state.childrenList];
        typeCopy = state.CopyList.filter((item)=>{
            return item.sex == payload 
        })
        state.childrenList = typeCopy
        return { ...state}

    case BAGLIST:
        let bagCopy = [...state.childrenList];
        bagCopy.forEach((item,idnex)=>{
            if(item.id == payload) {
                item.flag = !item.flag;
                if(item.flag) state.BagList.unshift(item)
            }
        })
        state.childrenList = bagCopy;
        Set_Item('BagList',state.BagList);
        Set_Item('childrenList',state.childrenList);
        return { ...state}

    case EDITBTN:
        let editCopy = [...state.BagList];
        editCopy.forEach((item,index)=>{
            if(item.id == payload.id) {
                item.inpFlag = !item.inpFlag;
            }
        })
        state.BagList = editCopy;
        Set_Item('BagList',state.BagList);
        return { ...state}

    case CLEARBTN:
        let ClearCopy = [...state.BagList];
        let ChildrenCopy = [...state.childrenList];
        for (let i = ClearCopy.length - 1; i > -1; i-- ){
            ClearCopy.forEach((item,index)=>{
                if(item.inpFlag) {
                    ClearCopy.splice(index,1)
                    ChildrenCopy.forEach((v,i)=>{
                        if(v.id == item.id) v.flag = !v.flag
                    })
                }
            })
        }
        state.BagList = ClearCopy;
        state.childrenList = ChildrenCopy;
        Set_Item('BagList',state.childrenList);
        Set_Item('BagList',state.BagList);
        return { ...state}

    // case LOVETYPE:
    //     return { ...state}

    default:
        return state
    }
}

export default legacy_createStore(routes,applyMiddleware(thunk,logger));
